/**
**G6 yangwg 20181204
*/
(function ($, Vue, doc) {
    'use strict';

    Vue.directive('sui-edocsignmark', {
        params: ['scope'],
        twoWay: true,
        bind: function () {
            var model = this.params.scope.model;
            var fieldInfo = this.params.scope.fieldInfo;
            if (!model || !fieldInfo) return;
            var that = this;
			
			//初始化赋值给value控件，用于解决如果加载完页面不点击任何签收操作提交,导致表单校验的时候无select签收编号(回退同)
			if((model.value == "" || (model.value != "" && model.value.indexOf("|") == -1)) && model.extMap.edocSignMark){
				if(model.extMap.edocSignMark.length > 0){
					model.value = model.extMap.edocSignMark[0].value;
				}					
			}				
            this.clickHandler = function (e) {
				if(e.target.localName == "button"){//如果点击的是切换按钮，动态切换样式
					if(_$("#edoc_sign_mark_input").style.display==""){
						_$("#edoc_sign_mark_input").style.display="none";
						_$("#edoc_sign_mark_select").style.display="";
						if(model.extMap.edocSignMark.length > 0){
							model.value = model.extMap.edocSignMark[0].value;
						}
					}else{
						_$("#edoc_sign_mark_input").style.display="";
						_$("#edoc_sign_mark_select").style.display="none";
					}
					
				}
            }.bind(this);
			
			this.clickHandlerChange = function (e) {//select控件监听
				if(e.target.localName == "select"){
					//console.log("执行change方法1");
				var selectMark=document.getElementById('edoc_sign_mark_select');
				var index=selectMark.selectedIndex;
				var val = selectMark.options[index].value;
				var signMarkList = this.params.scope.model.extMap.edocSignMark;
				for(var i = 0; i < signMarkList.length;i++){
					if(signMarkList[i].text == val){
						that.set(signMarkList[i].value);//循环服务器端返回签收编号，取value字段上传存储
						//console.log("执行change方法1-value" + signMarkList[i].value);
						summaryBO.summary.docMark2 = signMarkList[i].value;
						break;
					}
				}
				}
            }.bind(this);
			
			this.clickHandlerInput = function (e) {//签收编号录入监听入口
				if(e.target.localName == "input"){
				//console.log("执行input change方法2");
				var selectMark=document.getElementById('edoc_sign_mark_input');
				//var index=selectMark.selectedIndex;
				var val = selectMark.value;
				if(val.indexOf("|") != -1){
					alert("签收编号不能包含|");
					selectMark.value = "";
					return;
				}
				var submitVal = "1"+"|"+""+"|"+val+"|"+"";//拼装后台需要文号类型为  （手动标识|空|值|空）
				that.set(submitVal);
				summaryBO.summary.docMark2 = submitVal;
				//console.log("执行input change方法2-value" + submitVal);
				}
            }.bind(this);

            this.el.addEventListener('tap', this.clickHandler);
			this.el.addEventListener('change', this.clickHandlerChange);
			this.el.addEventListener('input', this.clickHandlerInput);

        },
        update: function (newValue, oldValue) {
            var model = this.params.scope.model;
            var fieldInfo = this.params.scope.fieldInfo;
            this.el.innerHTML = $.sui.htmlBuilder({value: newValue, desc: '', model: model, fieldInfo: fieldInfo});
            $.sui.clearErrorTips(this.el);
        },
        unbind: function () {
            this.el.removeEventListener('tap', this.clickHandler);
			this.el.removeEventListener('change', this.clickHandlerChange);
			this.el.removeEventListener('input', this.clickHandlerOnporpertychange);
        }
    })

})(cmp, Vue, document);